package com.example.anju.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.anju.entity.House;
import com.example.anju.entity.Result;
import com.example.anju.mapper.HouseMapper;

@Service
public class HouseService {
	@Autowired
	private HouseMapper houseMapper;

	// 根据house_user_id搜索房源
	public Result findHouseByUserId(House house) {
		Result result = new Result();
		result.setSuccess(false);
		result.setDatas(null);

		try {
			List<House> houseByUserId = houseMapper.findHouseByUserId(house.getHouse_user_id());
			if (houseByUserId.isEmpty()) {
				result.setMessage("该用户未发布房源");
			} else {
				result.setMessage("搜索房源成功");
				result.setSuccess(true);
				result.setDatas(houseByUserId);
			}
		} catch (Exception e) {
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return result;
	}

	// 根据location搜索房源
	public Result findHouseByLocation(House house) {
		Result result = new Result();
		result.setSuccess(false);
		result.setDatas(null);

		try {
			List<House> houseByLocation = houseMapper.findHouseByLocation(house.getLocation());
			if (houseByLocation.isEmpty()) {
				result.setMessage("该城市无房源");
			} else {
				result.setMessage("搜索房源成功");
				result.setSuccess(true);
				result.setDatas(houseByLocation);
			}
		} catch (Exception e) {
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return result;
	}

	// 根据address搜索房源
	public Result findHouseByAddress(House house) {
		Result result = new Result();
		result.setSuccess(false);
		result.setDatas(null);

		try {
			List<House> houseByAddress = houseMapper.findHouseByAddress(house.getAddress());
			if (houseByAddress.isEmpty()) {
				result.setMessage("该地区无房源");
			} else {
				result.setMessage("搜索房源成功");
				result.setSuccess(true);
				result.setDatas(houseByAddress);
			}
		} catch (Exception e) {
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return result;
	}

	// 根据time_type搜索房源
	public Result findHouseByTimeType(House house) {
		Result result = new Result();
		result.setSuccess(false);
		result.setDatas(null);

		try {
			List<House> houseByTimeType = houseMapper.findHouseByTimeType(house.getTime_type());
			if (houseByTimeType.isEmpty()) {
				result.setMessage("该租期类型无房源");
			} else {
				result.setMessage("搜索房源成功");
				result.setSuccess(true);
				result.setDatas(houseByTimeType);
			}
		} catch (Exception e) {
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return result;
	}

	// 根据home_type搜索房源
	public Result findHouseByHomeType(House house) {
		Result result = new Result();
		result.setSuccess(false);
		result.setDatas(null);

		try {
			List<House> houseByHomeType = houseMapper.findHouseByHomeType(house.getHome_type());
			if (houseByHomeType.isEmpty()) {
				result.setMessage("该租房类型无房源");
			} else {
				result.setMessage("搜索房源成功");
				result.setSuccess(true);
				result.setDatas(houseByHomeType);
			}
		} catch (Exception e) {
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return result;
	}

//	// 根据house_rent搜索房源
//	public Result findHouseByHouseRent(House house, int min, int max) {
//		Result result = new Result();
//		result.setSuccess(false);
//		result.setDatas(null);
//
//		try {
//			List<House> houseByHouseRent = houseMapper.findHouseByHouseRent(house.getHouse_rent(), min, max);
//			if (houseByHouseRent.isEmpty()) {
//				result.setMessage("该价格范围无房源");
//			} else {
//				result.setMessage("搜索房源成功");
//				result.setSuccess(true);
//				result.setDatas(houseByHouseRent);
//			}
//		} catch (Exception e) {
//			result.setMessage(e.getMessage());
//			e.printStackTrace();
//		}
//		return result;
//	}

	// 发布房源
	public Result publishHouse(House house) {
		Result result = new Result();
		result.setSuccess(false);
		result.setMessage("房源发布失败");
		result.setDatas(null);
		try {
			houseMapper.publishHouse(house);
			result.setMessage("房源发布成功");
			result.setSuccess(true);
		} catch (Exception e) {
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return result;
	}

	// 修改房源
	public Result changeHouseByHouseId(House house) {
		Result result = new Result();
		result.setSuccess(false);
		result.setDatas(null);
		try {
			List<House> houseByHouseId = houseMapper.findHouseByHouseId(house.getHouse_id());
			if (houseByHouseId.isEmpty()) {
				result.setMessage("房源详情查询失败");
			} else {
				houseMapper.changeHouseByHouseId(house);
				result.setMessage("房源修改成功");
				result.setSuccess(true);
				houseMapper.findHouseByHouseId(house.getHouse_id());
				result.setDatas(houseByHouseId);
			}
		} catch (Exception e) {
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return result;
	}
}
